package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.SafetyBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/24.
 */
public class SafetyCheckItemAdapter extends BaseExpandableListAdapter{

    private Context mContext;
    private List<SafetyBean> mDatas;
    private int retract;//缩进值

    public SafetyCheckItemAdapter(Context context, List<SafetyBean> data){
        this.mContext = context;
        this.mDatas = data;
        retract = (int)(context.getResources().getDisplayMetrics().density*10+0.5f);
    }

    @Override
    public int getGroupCount() {
        return mDatas!= null?mDatas.size():0;
    }

    @Override
    public int getChildrenCount(int childPosition) {
        return mDatas.get(childPosition).getData()!=null ? mDatas.get(childPosition).getData().size() : 0;
    }

    @Override
    public SafetyBean getGroup(int parentPosition) {
        return mDatas.get(parentPosition);
    }

    @Override
    public SafetyBean getChild(int parentPosition, int childPosition) {
        return mDatas.get(parentPosition).getData().get(childPosition);
    }

    @Override
    public long getGroupId(int parentPosition) {
        return parentPosition;
    }

    @Override
    public long getChildId(int parentPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 主菜单布局
     * @param parentPosition
     * @param isExpandabled  是否展开
     * @param view
     * @param viewGroup
     * @return
     */
    @Override
    public View getGroupView(int parentPosition, boolean isExpandabled, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if(view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.child_adapter,null);
            holder = new ViewHolder();
            holder.childName = (TextView)view.findViewById(R.id.childName);
            holder.img = (ImageView)view.findViewById(R.id.img);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }
        holder.childName.setText(mDatas.get(parentPosition).getName());
        view.setPadding(1*retract,0,0,0);
        return view;
    }
    class ViewHolder {

        private TextView childName;
        private ImageView img;
    }
    /**
     * 子菜单布局
     * @param parentPosition
     * @param childPosition
     * @param isExpandabled
     * @param view
     * @param viewGroup
     * @return
     */
    @Override
    public View getChildView(int parentPosition, int childPosition, boolean isExpandabled, View view, ViewGroup viewGroup) {
        ChildHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(
                    R.layout.child_child, null);
            holder = new ChildHolder();
            holder.secondName = (TextView) view.findViewById(R.id.secondName);
            holder.img = (ImageView) view.findViewById(R.id.img);
            holder.checkbox = (ImageView) view.findViewById(R.id.checkbox);
            view.setTag(holder);
        } else {
            holder = (ChildHolder) view.getTag();
        }
        holder.secondName.setText(mDatas.get(parentPosition).getData().get(childPosition).getName());
        if(mDatas.get(parentPosition).getData().get(childPosition).isSelected()){
            holder.checkbox.setVisibility(View.VISIBLE);
        }else{
            holder.checkbox.setVisibility(View.GONE);
        }
        view.setPadding(2*retract,0,0,0);
        return view;
    }
    class ChildHolder {

        private TextView secondName;
        private ImageView img, checkbox;
    }
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

}
